package cn.yshujia.controller;

import cn.yshujia.domain.dto.PageDTO;
import cn.yshujia.domain.entity.Label;
import cn.yshujia.domain.vo.ApiVO;
import cn.yshujia.domain.vo.ClassifyLabelVO;
import cn.yshujia.domain.vo.LabelVO;
import cn.yshujia.domain.vo.PageVO;
import cn.yshujia.service.impl.LabelServiceImpl;
import com.alibaba.fastjson2.JSON;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author YShuJia
 * @create 2024/4/23
 * @description 控制器
 */

@RestController
@Tag (name = "Label", description = "标签Api")
public class LabelController {
	
	@Resource
	private LabelServiceImpl service;
	
	@GetMapping ("/label/list/classify/{id}")
	@Operation (summary = "获取某个 分类ID 下的所有标签")
	public ApiVO<List<LabelVO>> classifyLabelList(@PathVariable Long id) {
		return ApiVO.success(service.selectListByClassifyId(id));
	}
	
	@GetMapping ("/label/list/classify/type/{type}")
	@Operation (summary = "获取某个 分类type 下的所有标签")
	public ApiVO<List<LabelVO>> list(@PathVariable Integer type) {
		return ApiVO.success(service.selectListByClassifyType(type));
	}
	
	@GetMapping ("/classify/label/tree/{id}")
	@Operation (summary = "通过 分类ID 获取分类、标签树信息")
	@PreAuthorize ("@sys.hasOnePermission('classify:all')")
	public ApiVO<ClassifyLabelVO> treeListById(@PathVariable Long id) {
		return ApiVO.success(service.getClassifyTreeById(id));
	}
	
	@GetMapping ("/classify/label/tree")
	@Operation (summary = "通过分类 type 获取分类、标签树信息")
	@PreAuthorize ("@sys.hasOnePermission('classify:all')")
	public ApiVO<List<ClassifyLabelVO>> treeList(@RequestParam Integer type) {
		return ApiVO.success(service.getClassifyTreeByType(type));
	}
	
	@GetMapping ("/admin/label/page")
	@Operation (summary = "admin 获取标签分页")
	@PreAuthorize ("@sys.hasOnePermission('label:all')")
	public ApiVO<PageVO<LabelVO>> list(@RequestParam String dto) {
		PageDTO pageDTO = JSON.parseObject(dto, PageDTO.class);
		if (null == pageDTO) {
			return ApiVO.error();
		}
		return ApiVO.success(service.adminPage(pageDTO));
	}
	
	
	@PostMapping ("/admin/label/insert")
	@Operation (summary = "admin 添加标签")
	@PreAuthorize ("@sys.hasOnePermission('label:all')")
	public ApiVO<Boolean> insert(@RequestBody Label label) {
		boolean bool = service.insert(label);
		if (bool) {
			return ApiVO.success(true);
		}
		return ApiVO.error();
	}
	
	@PutMapping ("/admin/label/update")
	@Operation (summary = "admin 更新标签")
	@PreAuthorize ("@sys.hasOnePermission('label:all')")
	public ApiVO<Boolean> update(@RequestBody Label label) {
		boolean result = service.update(label);
		if (result) {
			return ApiVO.success(true);
		}
		return ApiVO.error();
	}
	
	@DeleteMapping ("/admin/label/delete")
	@Operation (summary = "admin 删除标签")
	@PreAuthorize ("@sys.hasOnePermission('label:all')")
	public ApiVO<Boolean> remove(@RequestBody List<Long> ids) {
		boolean result = service.remove(ids);
		if (result) {
			return ApiVO.success(true);
		}
		return ApiVO.error();
	}
}


